﻿using System;
using System.Collections.Generic;

namespace Util.Data
{
    public static class DictionaryExtensions
    {
        public static TValue GetValueOrDefault<TKey, TValue>(
            this IDictionary<TKey, TValue> dictionary, TKey key)
        {
            return GetValueOrDefault<TKey, TValue>(dictionary, key, default(TValue));
        }

        public static TValue GetValueOrDefault<TKey, TValue>(
            this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");

            TValue value;
            if (dictionary.TryGetValue(key, out value))
                return value;
            else
                return defaultValue;
        }
    }
}
